package com.bootdo.system.service.impl;

import com.bootdo.common.dao.FileDao;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.ClassDO;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.bootdo.system.dao.ArticleDao;
import com.bootdo.system.domain.ArticleDO;
import com.bootdo.system.service.ArticleService;



@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private FileDao fileDao;

	
	@Override
	public ArticleDO get(Long id){
		ArticleDO articleDO = articleDao.get(id);
		if(StringUtils.isNotEmpty(articleDO.getAttachId())) {
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("attachId",articleDO.getAttachId());
			articleDO.setFilePathList(fileDao.list(map1));
		}
		if(StringUtils.isNotEmpty(articleDO.getSecondAttachId())) {
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("attachId",articleDO.getSecondAttachId());
			articleDO.setSecondFilePathList(fileDao.list(map1));
		}
		if(StringUtils.isNotEmpty(articleDO.getThirdAttachId())) {
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("attachId",articleDO.getThirdAttachId());
			articleDO.setThirdFilePathList(fileDao.list(map1));
		}
		return articleDO;
	}
	
	@Override
	public List<ArticleDO> list(Map<String, Object> map){
		return articleDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return articleDao.count(map);
	}
	
	@Override
	public int save(ArticleDO article){
		article.setCreateDate(new Date());
		article.setUpdateDate(new Date());
		article.setDelFlag("0");
		article.setViewCount(0l);
		article.setGoodCount(0l);
		return articleDao.save(article);
	}
	
	@Override
	public int update(ArticleDO article){
		article.setUpdateDate(new Date());
		return articleDao.update(article);
	}
	
	@Override
	public int remove(Long id){
		return articleDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return articleDao.batchRemove(ids);
	}

	/***
	 * 根据大类获取小类
	 * @param bigClass
	 * @return
	 */
	@Override
	public List<ClassDO> getSmallClass(String bigClass) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("bigClass",bigClass);
		List<ClassDO> list = articleDao.getSmallClass(map);
		return list;
	}

	@Override
	public List<ArticleDO> articleList(Map<String, Object> map) {
		List<ArticleDO> list = articleDao.list(map);
		if(!CollectionUtils.isEmpty(list)) {
			Map<String,Object> map1 = null;
			for(ArticleDO data : list) {
				if(StringUtils.isNotEmpty(data.getAttachId())) {
					map1 = new HashMap<String, Object>();
					map1.put("attachId",data.getAttachId());
					data.setFilePathList(fileDao.list(map1));
				}
				/*if(StringUtils.isNotEmpty(data.getSecondAttachId())) {
					map1 = new HashMap<String, Object>();
					map1.put("attachId",data.getSecondAttachId());
					data.setSecondFilePathList(fileDao.list(map1));
				}
				if(StringUtils.isNotEmpty(data.getThirdAttachId())) {
					map1 = new HashMap<String, Object>();
					map1.put("attachId",data.getThirdAttachId());
					data.setThirdFilePathList(fileDao.list(map1));
				}*/
			}
		}
		return list;
	}

	/**
	 * 获取文章详情
	 * @param id
	 * @return
	 */
	@Override
	public ArticleDO getDetail(Long id) {
		ArticleDO articleDO = articleDao.get(id);
		if(StringUtils.isNotEmpty(articleDO.getAttachId())) {
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("attachId",articleDO.getAttachId());
			articleDO.setFilePathList(fileDao.list(map1));
		}
		if(StringUtils.isNotEmpty(articleDO.getSecondAttachId())) {
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("attachId",articleDO.getSecondAttachId());
			articleDO.setSecondFilePathList(fileDao.list(map1));
		}
		if(StringUtils.isNotEmpty(articleDO.getThirdAttachId())) {
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("attachId",articleDO.getThirdAttachId());
			List<FileDO> thirdFilePathList = fileDao.list(map1);
			List<FileDO> list1 = new ArrayList<FileDO>();
			List<FileDO> list2 = new ArrayList<FileDO>();
			if(!CollectionUtils.isEmpty(thirdFilePathList)) {
				FileDO fileDO1 = new FileDO();
				FileDO fileDO2 = new FileDO();
				FileDO fileDO3 = new FileDO();
				FileDO fileDO4 = new FileDO();
				FileDO fileDO5 = new FileDO();
				FileDO fileDO6 = new FileDO();
				FileDO fileDO7 = new FileDO();
				FileDO fileDO8 = new FileDO();
				FileDO fileDO9 = new FileDO();
				FileDO fileDO10 = new FileDO();
				FileDO fileDO11 = new FileDO();
				FileDO fileDO12 = new FileDO();
				FileDO fileDO13 = new FileDO();
				FileDO fileDO14 = new FileDO();
				FileDO fileDO15 = new FileDO();
				FileDO fileDO16 = new FileDO();
				FileDO fileDO17 = new FileDO();
				FileDO fileDO18 = new FileDO();
				FileDO fileDO19 = new FileDO();
				FileDO fileDO20 = new FileDO();
				FileDO fileDO21 = new FileDO();
				FileDO fileDO22 = new FileDO();
				FileDO fileDO23 = new FileDO();
				for(FileDO data : thirdFilePathList) {
					//base64的值置为空，防止值过大，显示太慢
					data.setType(null);
					//不合理的需求，作品轮播图按找作品名称排序。。。
					//万鼎、李凯、乔建业、苗壮、白霜亮、惠亮、谭伟平、冯晓军、程雪峰、耿齐、曹波、李晓宁、韩勃正、李浩、倪超、白海红、武斌、李春燕、罗春波、蒲海红、赵拟、金鑫、李欣羽
					if(StringUtils.equals(data.getFileName(),"万鼎")) {
						fileDO1 = data;
					}else if(StringUtils.equals(data.getFileName(),"李凯")) {
						fileDO2 = data;
					}else if(StringUtils.equals(data.getFileName(),"乔建业")) {
						fileDO3 = data;
					}else if(StringUtils.equals(data.getFileName(),"苗壮")) {
						fileDO4 = data;
					}else if(StringUtils.equals(data.getFileName(),"白霜亮")) {
						fileDO5 = data;
					}else if(StringUtils.equals(data.getFileName(),"惠亮")) {
						fileDO6 = data;
					}else if(StringUtils.equals(data.getFileName(),"谭伟平")) {
						fileDO7 = data;
					}else if(StringUtils.equals(data.getFileName(),"冯晓军")) {
						fileDO8 = data;
					}else if(StringUtils.equals(data.getFileName(),"程雪峰")) {
						fileDO9 = data;
					}else if(StringUtils.equals(data.getFileName(),"耿齐")) {
						fileDO10 = data;
					}else if(StringUtils.equals(data.getFileName(),"曹波")) {
						fileDO11 = data;
					}else if(StringUtils.equals(data.getFileName(),"李晓宁")) {
						fileDO12 = data;
					}else if(StringUtils.equals(data.getFileName(),"韩勃正")) {
						fileDO13 = data;
					}else if(StringUtils.equals(data.getFileName(),"李浩")) {
						fileDO14 = data;
					}else if(StringUtils.equals(data.getFileName(),"倪超")) {
						fileDO15 = data;
					}else if(StringUtils.equals(data.getFileName(),"白海鸿")) {
						fileDO16 = data;
					}else if(StringUtils.equals(data.getFileName(),"武斌")) {
						fileDO17 = data;
					}else if(StringUtils.equals(data.getFileName(),"李春燕")) {
						fileDO18 = data;
					}else if(StringUtils.equals(data.getFileName(),"罗春波")) {
						fileDO19 = data;
					}else if(StringUtils.equals(data.getFileName(),"蒲海红")) {
						fileDO20 = data;
					}else if(StringUtils.equals(data.getFileName(),"赵拟")) {
						fileDO21 = data;
					}else if(StringUtils.equals(data.getFileName(),"金鑫")) {
						fileDO22 = data;
					}else if(StringUtils.equals(data.getFileName(),"李欣羽")) {
						fileDO23 = data;
					}else {
						list2.add(data);
					}
				}
				list1.add(fileDO1);
				list1.add(fileDO2);
				list1.add(fileDO3);
				list1.add(fileDO4);
				list1.add(fileDO5);
				list1.add(fileDO6);
				list1.add(fileDO7);
				list1.add(fileDO8);
				list1.add(fileDO9);
				list1.add(fileDO10);
				list1.add(fileDO11);
				list1.add(fileDO12);
				list1.add(fileDO13);
				list1.add(fileDO14);
				list1.add(fileDO15);
				list1.add(fileDO16);
				list1.add(fileDO17);
				list1.add(fileDO18);
				list1.add(fileDO19);
				list1.add(fileDO20);
				list1.add(fileDO21);
				list1.add(fileDO22);
				list1.add(fileDO23);
				list1.addAll(list2);
			}
			articleDO.setThirdFilePathList(list1);
		}
		return articleDO;
	}

}
